package emulator.analyzer.util;

import java.util.Arrays;

import emulator.util.AddressRange;

public class StatConverter
{
	public static boolean[] notZero(int [] field)
	{
		boolean[] result = new boolean [field.length];
		for (int i = 0; i < field.length; i++)
			result[i] = field[i] != 0;
		return result;
	}

	public static boolean[] rangeToMask(int start, int end, int length) {
		boolean[] result = new boolean [length];
		Arrays.fill(result, false);
		for (int i = start; i <= end; i++)
			result[i] = true;
		return result;
	}
	
	public static boolean[] rangeToMask(AddressRange range, int size)
	{
		return rangeToMask(range.getStart(), range.getEnd(), size);
	}
	
	public static boolean[] not(boolean[] field)
	{
		boolean[] result = new boolean [field.length];
		for (int i = 0; i < field.length; i++)
			result[i] = !field[i];
		return result;
	}

	public static boolean[] and(boolean[] field1, boolean[] field2)
	{
		boolean[] result = new boolean [field1.length];
		for (int i = 0; i < field1.length; i++)
			result[i] = field1[i] && field2[i];
		return result;
	}

	public static boolean[] or(boolean[] field1, boolean[] field2)
	{
		boolean[] result = new boolean [field1.length];
		for (int i = 0; i < field1.length; i++)
			result[i] = field1[i] || field2[i];
		return result;
	}
	
	public static int count(boolean [] field)
	{
		int count = 0;
		for (int i = 0; i < field.length; i++)
			if (field[i])
				count++;
		return count;
	}

	public static int first(boolean[] field) {
		for (int i = 0; i < field.length; i++)
			if (field[i])
				return i;
		return -1;
	}

	public static AddressRange getRangeAt(boolean[] field, int index)
	{
		int first = index;
		while (first > 0 && field[first-1])
			first--;
		int last = index;
		while (last < field.length-1 && field[last+1])
			last++;
		return new AddressRange(first, last);
	}
}
